//
//  ModifyAddressViewController.m
//  HWZB
//
//  Created by 许成雄 on 2017/4/24.
//  Copyright © 2017年 wonders. All rights reserved.
//

#import "ModifyAddressViewController.h"
#import "AddressPickerView.h"
#import "Context.h"
#import "NSString+Utils.h"
#import "RegexKit.h"
#import "MBProgressHUD+Toast.h"

@interface ModifyAddressViewController () <AddressPickerViewDelegate>
@property (strong, nonatomic) UIBarButtonItem *editButton;
@property (strong, nonatomic) UITextField *recipientTextField;
@property (strong, nonatomic) UITextField *mobilePhoneTextField;

@property (strong, nonatomic) UITextField *areaTextField;
@property (strong, nonatomic) UITextView *addressTextView;
@property (strong, nonatomic) AddressPickerView *addressPickerView;

@property (strong, nonatomic) NSString *provinceId;
@property (strong, nonatomic) NSString *cityId;
@property (strong, nonatomic) NSString *countyId;
@property (strong, nonatomic) NSString *partAddress;
@property (strong, nonatomic) NSString *area;
@property (strong, nonatomic) NSString *addressDesc;

@end

@implementation ModifyAddressViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.view.backgroundColor = I_COLOR_BACKGROUND;
    [self setBackNavgationItem];
    self.navigationItem.title = @"收货信息";
    
    [self createUI];
    [self loadData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

#pragma mark - AddressPickerViewDelegate
- (void)pickerView:(AddressPickerView *)pickerView didSelectAtProvince:(NSDictionary *)province city:(NSDictionary *)city county:(NSDictionary *)county {
    NSString *proviceTitle = (NSString *)[province objectForKey:@"province"];
    self.provinceId = (NSString *)[province objectForKey:@"provinceId"];
    NSString *cityTitle = (NSString *)[city objectForKey:@"city"];
    self.cityId = (NSString *)[city objectForKey:@"cityId"];
    NSString *countyTitle = (NSString *)[county objectForKey:@"county"];
    self.countyId = (NSString *)[county objectForKey:@"countyId"];
    NSString *addressStr = [NSString stringWithFormat:@"%@%@%@", proviceTitle, cityTitle, countyTitle];
    self.areaTextField.text = addressStr;
}

#pragma mark - Private Method
- (void)editButtonAction {
    [self.recipientTextField resignFirstResponder];
    [self.mobilePhoneTextField resignFirstResponder];
    NSString *username = self.recipientTextField.text;
    if([NSString isBlankString:username]) {
        [MBProgressHUD showToast:@"请输入收件人名称" initWithView:self.view];
        return;
    }
    NSString *mobile = self.mobilePhoneTextField.text;
    if([NSString isBlankString:mobile]) {
        [MBProgressHUD showToast:@"请输入电话号码" initWithView:self.view];
        return;
    } else if(![RegexKit validateMobile:mobile]) {
        [MBProgressHUD showToast:@"请输入正确的电话号码" initWithView:self.view];
        return;
    }
    NSString *partAddress = self.areaTextField.text;
    if([NSString isBlankString:partAddress]) {
        [MBProgressHUD showToast:@"请选择所在地区" initWithView:self.view];
        return;
    }
    NSString *areaStr = self.addressTextView.text;
    if([NSString isBlankString:areaStr]) {
        [MBProgressHUD showToast:@"请输入详细地址" initWithView:self.view];
        return;
    }
    self.partAddress = partAddress;
    self.area = areaStr;
    self.addressDesc = [NSString stringWithFormat:@"%@%@", partAddress, areaStr];
    //TODO -- 赋值
    [Context sharedInstance].recipientsProvinceId = self.provinceId;
    [Context sharedInstance].recipientsCityId = self.cityId;
    [Context sharedInstance].recipientsCountyId = self.countyId;
    [Context sharedInstance].recipientsArea = self.area;
    [Context sharedInstance].recipientsAddress = self.addressDesc;
    [Context sharedInstance].recipientsName = username;
    [Context sharedInstance].recipientsProvinceId = mobile;
    
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)addressTapAction {
    [self.recipientTextField resignFirstResponder];
    [self.mobilePhoneTextField resignFirstResponder];
    if(self.addressPickerView.hidden) {
        self.addressPickerView.hidden = NO;
    } else {
        self.addressPickerView.hidden = YES;
    }
}

- (void)createUI {
    
    UIButton *editCustomView = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 19 + 6, 22)];
    editCustomView.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    [editCustomView setImage:[UIImage imageNamed:@"ic_me_edit"] forState:UIControlStateNormal];
    [editCustomView setImage:[UIImage imageNamed:@"ic_me_edit"] forState:UIControlStateFocused];
    [editCustomView setImage:[UIImage imageNamed:@"ic_me_edit"] forState:UIControlStateSelected];
    [editCustomView addTarget:self action:@selector(editButtonAction) forControlEvents:UIControlEventTouchUpInside];
    self.editButton = [[UIBarButtonItem alloc] initWithCustomView:editCustomView];
    self.navigationItem.rightBarButtonItem = self.editButton;

    
    UIView *sectionView1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, TRANS_VALUE(40.0f) * 2 + 2.0f)];
    sectionView1.backgroundColor = I_COLOR_WHITE;
    [self.view addSubview:sectionView1];
    [self createViewsInSectionView1:sectionView1];
    
    UIView *sectionView2 = [[UIView alloc] initWithFrame:CGRectMake(0, TRANS_VALUE(40.0f) * 2 + 2.0f + TRANS_VALUE(10.0f), SCREEN_WIDTH, TRANS_VALUE(150.0f))];
    sectionView2.backgroundColor = I_COLOR_WHITE;
    [self.view addSubview:sectionView2];
    [self createViewsInSectionView2:sectionView2];
    
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, TRANS_VALUE(40.0f) * 2 + 3.0f + TRANS_VALUE(10.0f) + TRANS_VALUE(148.0f), SCREEN_WIDTH, SCREEN_HEIGHT - 64.0f - (TRANS_VALUE(40.0f) * 2 + 3.0f + TRANS_VALUE(10.0f) + TRANS_VALUE(148.0f)))];
    headerView.backgroundColor = I_COLOR_WHITE;
    [self.view addSubview:headerView];
    
    self.addressPickerView = [[AddressPickerView alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT - 64.0f - 240.0f, SCREEN_WIDTH, 240.0f)];
    [self.view addSubview:self.addressPickerView];
    self.addressPickerView.delegate = self;
    self.addressPickerView.hidden = YES;
    
}

- (void)createViewsInSectionView1:(UIView *)sectionView1 {
    UILabel *recipientLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, TRANS_VALUE(80.0f), TRANS_VALUE(40.0f))];
    recipientLabel.font = [UIFont systemFontOfSize:TRANS_VALUE(13.0f)];
    recipientLabel.textColor = I_COLOR_BLACK;
    recipientLabel.textAlignment = NSTextAlignmentRight;
    recipientLabel.text = @"收件人:";
    [sectionView1 addSubview:recipientLabel];
    
    self.recipientTextField = [[UITextField alloc] initWithFrame:CGRectMake(TRANS_VALUE(90.0f), TRANS_VALUE(0.0f), TRANS_VALUE(220.0f), TRANS_VALUE(40.0f))];
    self.recipientTextField.textAlignment = NSTextAlignmentLeft;
    self.recipientTextField.backgroundColor = I_COLOR_WHITE;
    self.recipientTextField.font = [UIFont systemFontOfSize:TRANS_VALUE(13.0f)];
    self.recipientTextField.borderStyle = UITextBorderStyleNone;
    self.recipientTextField.textColor = I_COLOR_BLACK;
    self.recipientTextField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"请输入收件人" attributes:@{NSForegroundColorAttributeName:I_COLOR_GRAY}];
    self.recipientTextField.tintColor = I_COLOR_BLACK;
    [sectionView1 addSubview:self.recipientTextField];
    
    UIView *dividerView1 = [[UIView alloc] initWithFrame:CGRectMake(0, TRANS_VALUE(40.0f), SCREEN_WIDTH, 1.0f)];
    dividerView1.backgroundColor = I_DIVIDER_COLOR;
    [sectionView1 addSubview:dividerView1];
    
    UILabel *mobilePhoneLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, TRANS_VALUE(40.0f) + 1.0f, TRANS_VALUE(80.0f), TRANS_VALUE(40.0f))];
    mobilePhoneLabel.font = [UIFont systemFontOfSize:TRANS_VALUE(13.0f)];
    mobilePhoneLabel.textColor = I_COLOR_BLACK;
    mobilePhoneLabel.textAlignment = NSTextAlignmentRight;
    mobilePhoneLabel.text = @"联系方式:";
    [sectionView1 addSubview:mobilePhoneLabel];
    
    self.mobilePhoneTextField = [[UITextField alloc] initWithFrame:CGRectMake(TRANS_VALUE(90.0f), TRANS_VALUE(40.0f) + 1.0f, TRANS_VALUE(220.0f), TRANS_VALUE(40.0f))];
    self.mobilePhoneTextField.textAlignment = NSTextAlignmentLeft;
    self.mobilePhoneTextField.backgroundColor = I_COLOR_WHITE;
    self.mobilePhoneTextField.borderStyle = UITextBorderStyleNone;
    self.mobilePhoneTextField.textColor = I_COLOR_BLACK;
    self.mobilePhoneTextField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"请输入电话号码" attributes:@{NSForegroundColorAttributeName:I_COLOR_GRAY}];
    self.mobilePhoneTextField.tintColor = I_COLOR_BLACK;
    self.mobilePhoneTextField.font = [UIFont systemFontOfSize:TRANS_VALUE(13.0f)];
    [sectionView1 addSubview:self.mobilePhoneTextField];
    self.mobilePhoneTextField.keyboardType = UIKeyboardTypeNumberPad;
    
    UIView *dividerView2 = [[UIView alloc] initWithFrame:CGRectMake(0, TRANS_VALUE(40.0f) * 2 + 1.0f, SCREEN_WIDTH, 1.0f)];
    dividerView2.backgroundColor = I_DIVIDER_COLOR;
    [sectionView1 addSubview:dividerView2];
}

- (void)createViewsInSectionView2:(UIView *)sectionView2 {
    UILabel *areaLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, TRANS_VALUE(80.0f), TRANS_VALUE(40.0f))];
    areaLabel.font = [UIFont systemFontOfSize:TRANS_VALUE(13.0f)];
    areaLabel.textColor = I_COLOR_BLACK;
    areaLabel.textAlignment = NSTextAlignmentRight;
    areaLabel.text = @"所在地区:";
    [sectionView2 addSubview:areaLabel];
    
    self.areaTextField = [[UITextField alloc] initWithFrame:CGRectMake(TRANS_VALUE(90.0f), TRANS_VALUE(0.0f), TRANS_VALUE(210.0f), TRANS_VALUE(40.0f))];
    self.areaTextField.textAlignment = NSTextAlignmentLeft;
    self.areaTextField.backgroundColor = I_COLOR_WHITE;
    self.areaTextField.borderStyle = UITextBorderStyleNone;
    self.areaTextField.textColor = I_COLOR_BLACK;
    self.areaTextField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"请输入所在地区" attributes:@{NSForegroundColorAttributeName:[UIColor darkGrayColor]}];
    self.areaTextField.tintColor = I_COLOR_BLACK;
    self.areaTextField.font = [UIFont systemFontOfSize:TRANS_VALUE(13.0f)];
    [sectionView2 addSubview:self.areaTextField];
    self.areaTextField.enabled = NO;
    
    UIImageView *arrowImageView = [[UIImageView alloc] initWithFrame:CGRectMake(TRANS_VALUE(308.0f), TRANS_VALUE(15.0f), TRANS_VALUE(6.0f), TRANS_VALUE(10.0f))];
    arrowImageView.contentMode = UIViewContentModeScaleAspectFit;
    [sectionView2 addSubview:arrowImageView];
    arrowImageView.image = [UIImage imageNamed:@"ic_me_arrow_black"];
    sectionView2.userInteractionEnabled = YES;
    UITapGestureRecognizer *tapAction  = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(addressTapAction)];
    [sectionView2 addGestureRecognizer:tapAction];
    
    UIView *dividerView1 = [[UIView alloc] initWithFrame:CGRectMake(0, TRANS_VALUE(40.0f), SCREEN_WIDTH, 1.0f)];
    dividerView1.backgroundColor = I_COLOR_DIVIDER;
    [sectionView2 addSubview:dividerView1];
    
    self.addressTextView = [[UITextView alloc] initWithFrame:CGRectMake(TRANS_VALUE(10.0f), TRANS_VALUE(40.0f) + TRANS_VALUE(10.0f), SCREEN_WIDTH - 2 * TRANS_VALUE(10.0f), TRANS_VALUE(90.0f))];
    self.addressTextView.backgroundColor = I_COLOR_WHITE;
    self.addressTextView.layer.borderColor = I_COLOR_DIVIDER.CGColor;
    self.addressTextView.layer.borderWidth = 0.5f;
    self.addressTextView.textColor = I_COLOR_BLACK;
    self.addressTextView.font = [UIFont systemFontOfSize:TRANS_VALUE(13.0f)];
    [sectionView2 addSubview:self.addressTextView];
    
    UIView *dividerView2 = [[UIView alloc] initWithFrame:CGRectMake(0, TRANS_VALUE(150.0f) - 1.0f, SCREEN_WIDTH, 1.0f)];
    dividerView2.backgroundColor = I_COLOR_DIVIDER;
    [sectionView2 addSubview:dividerView2];
}

- (void)loadData {
    
}

@end
